package com.wonghiu.framework.exchange;

import com.wonghiu.framework.common.enums.HRpcResponseCodeEnum;
import lombok.*;

import java.io.Serializable;

/**
 * @Author wonghiu
 * @Date 2021/8/10 19:57
 * @Version 1.0.1
 */
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@ToString
public class HRpcResponse<T> implements Serializable {

    private static final long serialVersionUID = 715745410605631233L;
    private String requestId;

    private Integer code;

    private String message;

    private T data;

    public static <T> HRpcResponse<T> success(T data, String requestId) {
        HRpcResponse<T> response = new HRpcResponse<>();
        response.setCode(HRpcResponseCodeEnum.SUCCESS.getCode());
        response.setMessage(HRpcResponseCodeEnum.SUCCESS.getMessage());
        response.setRequestId(requestId);
        if (null != data) {
            response.setData(data);
        }
        return response;
    }

    public static <T> HRpcResponse<T> fail(HRpcResponseCodeEnum rpcResponseCodeEnum) {
        HRpcResponse<T> response = new HRpcResponse<>();
        response.setCode(rpcResponseCodeEnum.getCode());
        response.setMessage(rpcResponseCodeEnum.getMessage());
        return response;
    }

}